﻿@inherits AdminCompontentBase
<MCard Class="d-flex flex-column px-2 rounded-4">
    <MCardTitle class="px-0 py-1">
        <MListItem>
            <MListItemTitle Class="overflow-visible">
                @if (MessageTask.Channel != null)
                {
                    <ColorTag Color="@(MessageTask.IsEnabled?MessageTask.Channel?.Color:"regular3")">@MessageTask.Channel?.DisplayName</ColorTag>
                }
            </MListItemTitle>
            <MListItemIcon Class="mr-n2">
                <MMenu OffsetY>
                    <ActivatorContent>
                        <SIcon @attributes="@context.Attrs" Size=26 Class="emphasis2--text">mdi-dots-horizontal</SIcon>
                    </ActivatorContent>
                    <ChildContent>
                        <MList Dense>
                            <MListItem>
                                <MListItemContent>
                                    <MListItemTitle>
                                        <BooleanButtonGroup Disabled="@MessageTask.IsDraft" Value="@MessageTask.IsEnabled" StyleType="StyleTypes.Slider" ValueChanged="HandleOnIsEnabled" />
                                    </MListItemTitle>
                                </MListItemContent>
                            </MListItem>
                            @if (MessageTask.IsDraft)
                            {
                                <MListItem Link OnClick="HandleOnEdit">
                                    <MListItemContent>
                                        <MListItemTitle>
                                            <span class="body regular--text">@T("Edit")</span>
                                        </MListItemTitle>
                                    </MListItemContent>
                                </MListItem>
                            }
                            else
                            {
                                <MListItem Link OnClick="HandleOnView">
                                    <MListItemContent>
                                        <MListItemTitle>
                                            <span class="body regular--text">@T("Permission.ViewMessageTask")</span>
                                        </MListItemTitle>
                                    </MListItemContent>
                                </MListItem>
                            }
                            <MListItem Link OnClick="HandleOnTest">
                                <MListItemContent>
                                    <MListItemTitle>
                                        <span class="body regular--text">@T("Test")</span>
                                    </MListItemTitle>
                                </MListItemContent>
                            </MListItem>
                            <MListItem Link OnClick="HandleOnDelete">
                                <MListItemContent>
                                    <MListItemTitle>
                                        <span class="body error--text">@T("Delete")</span>
                                    </MListItemTitle>
                                </MListItemContent>
                            </MListItem>
                        </MList>
                    </ChildContent>
                </MMenu>

            </MListItemIcon>
        </MListItem>
    </MCardTitle>
    <MDivider></MDivider>
    <MCardText class="flex-grow-1 px-0 d-flex flex-column pb-0">
        <div class="@($"h6 text-truncate {(MessageTask.IsEnabled?"emphasis--text":"regular3--text")}")" style="line-height: 24px;">@MessageTask.DisplayName</div>
        <div class="caption regular3--text" style="line-height: 18px;">@((MessageTask.SendTime?.ToOffset(JsInitVariables.TimezoneOffset) ?? MessageTask.ExpectSendTime?.ToOffset(JsInitVariables.TimezoneOffset))?.ToString(T("$DateTimeFormat")))</div>
        <Clamp Class="@($"flex-grow-1 rounded fill-background body px-2 py-1 text-justify mt-4 {(MessageTask.IsEnabled?"regular--text":"regular3--text")}")">
           @MessageTask.Content
        </Clamp>
    </MCardText>
    <MCardActions class="px-3 py-4">
        <span class="@($"caption {(MessageTask.IsEnabled?"emphasis2--text":"regular3--text")}")">@T($"DisplayName.MessageEntityType.{((MessageEntityTypes)MessageTask.EntityType).ToString()}")</span>
        @if (MessageTask.IsDraft == false)
        {
            <span class="d-flex px-1" style="height: 8px">
                <MDivider Vertical />
            </span>
            <MessageTaskStatus Value="@MessageTask.Status"></MessageTaskStatus>
        }
        <MSpacer />
        <span class="caption regular3--text">@MessageTask.ModificationTime.Humanize(culture:I18n.Culture) </span>
        <span class="d-flex px-1" style="height: 8px">
            <MDivider Vertical />
        </span>
        <span class="@($"caption {(MessageTask.IsEnabled?"regular2--text":"regular3--text")}")">@MessageTask.ModifierName</span>
    </MCardActions>
</MCard>

@code {
    [EditorRequired]
    [Parameter]
    public MessageTaskDto MessageTask { get; set; } = null!;

    [Parameter]
    public EventCallback OnEdit { get; set; }

    [Parameter]
    public EventCallback OnView { get; set; }

    [Parameter]
    public EventCallback OnTest { get; set; }

    [Parameter]
    public EventCallback OnDelete { get; set; }

    [Parameter]
    public EventCallback OnIsEnabled { get; set; }

    protected override string? PageName { get; set; } = "MessageTaskBlock";

    private async Task HandleOnEdit()
    {
        if (OnEdit.HasDelegate)
        {
            await OnEdit.InvokeAsync();
        }
    }

    private async Task HandleOnView()
    {
        if (OnView.HasDelegate)
        {
            await OnView.InvokeAsync();
        }
    }

    private async Task HandleOnTest()
    {
        if (OnTest.HasDelegate)
        {
            await OnTest.InvokeAsync();
        }
    }

    private async Task HandleOnDelete()
    {
        if (OnDelete.HasDelegate)
        {
            await OnDelete.InvokeAsync();
        }
    }

    private async Task HandleOnIsEnabled()
    {
        if (OnIsEnabled.HasDelegate)
        {
            await OnIsEnabled.InvokeAsync();
        }
    }
}